const user = require("../sql/user");
const uuid = require("uuid");
const md5 = require("md5");

module.exports = (req, res)=>{
    // 解析前端发送的数据
    const un = req.body.username;
    const pw = req.body.password;
    // 先根据用户名从数据库中读
    user.find({username: un}, (err, data)=>{
        if(data.length > 0){
            // 读到了，表示用户名重名
            res.send("用户名重名");
        }else{
            // 没读到，准备存储到数据库
            user.insertMany({
                userId: uuid.v1(),
                username: un,
                password: md5(pw),
                power: 0,
                hobby: [],
                tel: "",
                avatar: "",
                birthday: 0,
                regTime: Date.now(),
                lastLoginTime: []
            }, err=>{
                res.send("注册成功");
            })
        }
    });
}
